package com.aiti.lulian.utils;

import org.springframework.util.StringUtils;

import java.security.MessageDigest;

public class Md5Util {

    public static void main(String[] args) {
        System.out.println(hash("18353185696"));
    }

    private static byte[] md5(String s) {
        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(s.getBytes("UTF-8"));
            byte[] messageDigest = algorithm.digest();
            return messageDigest;
        } catch (Exception var3) {
            return null;
        }
    }

    private static final String toHex(byte[] hash) {
        if (hash == null) {
            return null;
        } else {
            StringBuffer buf = new StringBuffer(hash.length * 2);

            for(int i = 0; i < hash.length; ++i) {
                if ((hash[i] & 255) < 16) {
                    buf.append("0");
                }

                buf.append(Long.toString((long)(hash[i] & 255), 16));
            }

            return buf.toString();
        }
    }

    public static String hash(String s) {
        if(org.apache.commons.lang3.StringUtils.isBlank(s)){
            return "";
        }
        try {
            return new String(toHex(md5(s)).getBytes("UTF-8"), "UTF-8");
        } catch (Exception var2) {
            return s;
        }
    }

    public static String generatePasswordMD5(String input, String salt) {
        if (StringUtils.isEmpty(salt)) {
            salt = "";
        }
        return hash(salt + md5(input));
    }



}
